package NIO;
import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;
import java.util.Scanner;

public class write {

	static String pathfile;
	static String a;
	private static Scanner sc;
	public static void setpath(String d){
		pathfile=d;
	}
	public static void setdulieu(String c){
		a=c;
	}
	public static void writetofile(){
		File aFile = new File(pathfile);
	    FileOutputStream outputFile = 
	      null;                          // Place to store the stream reference
	    try {
	      outputFile = new FileOutputStream(aFile, true);
	      System.out.println("File stream created successfully.");
	    } catch (FileNotFoundException e) {
	      e.printStackTrace(System.err);
	    } 
        FileChannel outChannel = outputFile.getChannel();
	    ByteBuffer buf = ByteBuffer.allocate(1024);
	    for (int i = 0; i < a.length(); i++) {
		      buf.putChar(a.charAt(i));
		    }
	    buf.flip();
	    try {
		      outChannel.write(buf); 
		      outputFile.close();     
		    } catch (IOException e) {
		      e.printStackTrace(System.err);
		    }
	}
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		System.out.print("nhap duong đan file");
		setpath(sc.nextLine());
		System.out.print("du lieu ban muon ghi file");
		setdulieu(sc.nextLine());
		writetofile();
	}
	
}
